// 获取上个页面实例
const prePage = ()=>{
	let pages = getCurrentPages();
	let prePage = pages[pages.length - 2];
	// #ifdef H5
	return prePage;
	// #endif
	return prePage.$vm;
}

/* 常用正则 */
let regExps = {
	email: /^[0-9a-zA-Z_]+@[0-9a-zA-Z_]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$/, //邮箱
	mobile: /^(?:1\d{2})-?\d{5}(\d{3}|\*{3})$/, //手机号码
	qq: /^[1-9][0-9]{4,9}$/, //QQ
	befitName: /^[a-z0-9A-Z\u4e00-\u9fa5]+$/, //合适的用户名，中文,字母,数字
	befitPwd: /^[a-z0-9A-Z_]+$/, //合适的密码，字母,数字,下划线
	allNumber: /^[0-9]+.?[0-9]$/ //全部为数字
}

/* Loading加载框 */
let showLoading = (title = '加载中') => {
	uni.showLoading({
		title,
		mask: true
	});
}

// 消息提示
const msg = (title = '', duration=1500, mask=false, icon='none',position='center')=>{
	uni.showToast({
		title,
		icon,
		duration,
		mask,
		position
	});
}

// 跳转封装
const navigate = (options,type = "navigateTo")=>{
	switch (type){
		case "navigateTo":
		uni.navigateTo(options);
			break;
		case "redirectTo":
		uni.redirectTo(options);
			break;
		case "reLaunch":
		uni.reLaunch(options);
			break;
		case "switchTab":
		uni.switchTab(options);
			break;
	}
}

/* 保留任意位小数
 * num：值
 * decimal：保留几位小数
 */
const formatDecimal = (num, decimal) => {
  num = num.toString()
  let index = num.indexOf('.')
  if (index !== -1) {
    num = num.substring(0, decimal + index + 1)
  } else {
    num = num.substring(0)
  }
  return parseFloat(num).toFixed(decimal)
}

export default {
	regExps,
	showLoading,
	msg,
	prePage,
	navigate,
	formatDecimal
}
